import { login,tokenlogin, sgmwlogin,sgmwloginjk,logout, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import qs from 'qs';

const state = {
  token: getToken(),
  name: '',
  avatar: ''
}
const config = {
  grant_type:'password',
  client_id:'00000000-0000-0000-0000-000000000000',
  jxsh:'',
  username:'',
  password:'',
  usertype:'' 
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {  

    return new Promise((resolve, reject) => {
      //方式一
      // let form = new FormData();
      // form.append('grant_type', 'client_credentials');
      // form.append('username', 'sgmwadmin');
      // form.append('password', 'P@ssw0rd');
      // form.append('client_id', '2961C96D-4DB0-4FCF-99FA-FE18AC9A496A');
      // let param = new URLSearchParams(form);
      config.jxsh=userInfo.Jxsh;
      config.username=userInfo.LoginName;
      config.password=userInfo.password;
      config.usertype=userInfo.UserType;
      let strData=qs.stringify(config)

      tokenlogin(strData).then(res=>{    
        commit('SET_TOKEN', res.access_token)
        setToken(res.access_token)
        resolve(res)
      }).catch(error => {
          reject(error)
      })
  
      // login(userInfo).then(response => {
      //   const { data } = response 
      //   commit('SET_TOKEN', data.Token)
      //   commit('SET_NAME', data.User.UserName)
      //   commit('SET_AVATAR', data.User.HeadPhoto)
      //   setToken(data.Token)
      //   resolve(data)

      // }).catch(error => {
      //   reject(error)
      // })

    })

  },
  //经销商登录
  sgmwlogin({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      sgmwlogin(userInfo).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.Token)
        commit('SET_NAME', data.User.UserName)
        commit('SET_AVATAR', data.User.HeadPhoto)
        setToken(data.Token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  sgmwloginjk({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      sgmwloginjk(userInfo).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.Token)
        commit('SET_NAME', data.User.UserName)
        commit('SET_AVATAR', data.User.HeadPhoto)
        setToken(data.Token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },


  getUserInfoByUPN({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(response => {
        const { data } = response
        commit('SET_NAME', data.UserName)
        commit('SET_AVATAR', data.HeadPhoto)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
      // commit('SET_TOKEN', username)
      // setToken(username)
    })
  },

  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getInfo(state.token).then(response => {
  //       const { data } = response

  //       if (!data) {
  //         reject('Verification failed, please Login again.')
  //       }

  //       const { name, avatar } = data

  //       commit('SET_NAME', name)
  //       commit('SET_AVATAR', avatar)
  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

